<?php
require_once(FWS_DIR_MODEL_DAO.'teammodeldao.class.php');
require_once(FWS_DIR_MODEL.'teammodel.class.php');

class CompetitionModelDao {

	function CompetitionModelDao() {
	}

	function findAllCompetition() {
		$competition = array();
		$query  = "SELECT ID_COMPETITION, LIB_COMPETITION, SEASON ";
		$query .= "FROM TBL_COMPETITION ORDER BY SEASON, LIB_COMPETITION ";
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
		while ($row = mysql_fetch_assoc($result)) {
			$competition[] = new CompetitionModel($row); 
		}
		return $competition;
	}

   function findAllChamp() {
		$compet = array();
		$query  = "SELECT C.ID_COMPETITION, C.SEASON, C.LIB_COMPETITION "
			."FROM TBL_COMPETITION C "
			."WHERE C.TYP_COMPETITION = 'CHAMPIONNAT' ORDER BY C.LIB_COMPETITION";
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
		while ($row = mysql_fetch_row($result)) {
			$compet[$row[0]] = $row[1] . " - " . $row[2]; 
		}
		return $compet;
	}

	function findChampForCurrentSeason() {
		$compet = array();
		$query  = "SELECT C.ID_COMPETITION, C.SEASON, C.LIB_COMPETITION "
			."FROM TBL_COMPETITION C "
			."INNER JOIN TBL_SEASON S ON C.SEASON=S.SEASON "
			."WHERE C.TYP_COMPETITION = 'CHAMPIONNAT' "
			."AND S.CURRENT = 1 "
			."ORDER BY C.ORDER_AFFIC";
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
		while ($row = mysql_fetch_row($result)) {
			$compet[$row[0]] = $row[1] . " - " . $row[2]; 
		}
		return $compet;
	}
	
    function findCompetitionBySeason($season) {
		$compet = array();
		$query  = "SELECT C.ID_COMPETITION, C.SEASON, C.LIB_COMPETITION "
			."FROM TBL_COMPETITION C "
			."WHERE C.SEASON = '$season' ORDER BY C.ORDER_AFFIC";
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
		while ($row = mysql_fetch_row($result)) {
			$compet[$row[0]] = $row[1] . " - " . $row[2]; 
		}
		return $compet;
	}
	
	function findCompetitionForCurrentSeason() {
		$compet = array();
		$query  = "SELECT C.ID_COMPETITION, C.SEASON, C.LIB_COMPETITION "
			."FROM TBL_COMPETITION C "
			."INNER JOIN TBL_SEASON S ON C.SEASON=S.SEASON "
			."WHERE S.CURRENT = 1 ORDER BY C.ORDER_AFFIC";
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
		while ($row = mysql_fetch_row($result)) {
			$compet[$row[0]] = $row[1] . " - " . $row[2]; 
		}
		return $compet;
	}
	
	function findCompetitionForCurrentSeasonExceptChamp() {
		$compet = array();
		$query  = "SELECT C.ID_COMPETITION, C.SEASON, C.LIB_COMPETITION "
			."FROM TBL_COMPETITION C "
			."INNER JOIN TBL_SEASON S ON C.SEASON=S.SEASON "
			."WHERE S.CURRENT = 1 AND C.TYP_COMPETITION != 'CHAMPIONNAT' ORDER BY C.LIB_COMPETITION";
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
		while ($row = mysql_fetch_row($result)) {
			$compet[$row[0]] = $row[1] . " - " . $row[2]; 
		}
		return $compet;
	}

	function queryCompetitionById($id_competition) {
		$query  = "SELECT ID_COMPETITION, LIB_COMPETITION, SEASON ";
		$query .= "FROM TBL_COMPETITION ";
		$query .= "WHERE ID_COMPETITION=".$id_competition;
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
		$row = mysql_fetch_assoc($result);
		$competition = new CompetitionModel($row); 
		return $competition;
	}

	function queryCompetitionBySeason($season) {
		$competitions=array();
		$query  = "SELECT ID_COMPETITION, LIB_COMPETITION, SHORT_LIB ";
		$query .= "FROM TBL_COMPETITION ";
		$query .= "WHERE SEASON='".$season."' ORDER BY ORDER_AFFIC";
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
		
		while($row = mysql_fetch_assoc($result)) {
			$competitions[] = new CompetitionModel($row); 
		}
		return $competitions;
	}

	
	
	function updateCompetition($competition) {
		$competition = array();
		$query  = "UPDATE TBL_COMPETITION SET ";
		$query .= "LIB_COMPETITION='".$competition->getLibCompetition();
		$query .= "', SEASON='".$competition->getSaison();
		$query .= "' WHERE ID_COMPETITION=".$competition->getIdCompetition();
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
	}
	
	function insertCompetition($competition) {
		$competition = array();
		$query  = "INSERT INTO TBL_COMPETITION(LIB_COMPETITION,";
		$query .= "SEASON) ";
		$query .= "VALUES('".$competition->getLibCompetition()."', '";
		$query .= $competition->getSaison()."')";
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
	}
	
	function addTeam($competition) {
		$competition = array();
		$query  = "INSERT INTO TEAM_HAS_COMPETITION(ID_COMPETITION,";
		$query .= "ID_TEAM) ";
		$query .= "VALUES('".$competition->getIdCompetition()."', '";
		$query .= $competition->getIdTeam()."')";
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
	}

	function getTeams($id_competition){
		$query  = "SELECT T.LIB_TEAM, TC.ID_TEAM, EC.ID_COMPETITION ";
		$query .= "FROM TEAM_HAS_COMPETITION TC INNER JOIN TBL_TEAM T ON TC.ID_TEAM = T.ID_TEAM ";
		$query .= "WHERE ID_COMPETITION=".$id_competition." ORDER BY T.LIB_TEAM";
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
		while ($row = mysql_fetch_assoc($result)) {
			$competition[] = new CompetitionModel($row); 
		}
		return $competition;
	}
	
	function savepreparechamp($toto) {
		foreach ($toto['edit'] as $id)
		{
			$query  = "INSERT INTO TEAM_HAS_COMPETITION (ID_TEAM, ID_COMPETITION) VALUES ('".$id;
			$query .= "', '".$toto['id_competition']."')" ;
		
			$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
						
		}
	}


	
	function GenererChampionnat($id_competition_) {
		$id_competition=23;
		$_competitionmodeldao = new CompetitionModelDao();
		$competition = $_competitionmodeldao->queryCompetitionById($id_competition);
	    //$competition = this->queryCompetitionById($id_competition);
	    $_teammodeldao = new TeamModelDao();
	    // principe :
    	// on recup�e toutes les �uipes d'un championnat que l'on parcourt
    	// Pour chacune d'elle on cr� les matchs o elle accueille les autres �uipes
    	// => 1 ligne par match dans TBL_MATCH
    	// => 2 lignes par match dans TBL_GAMEPLAY
    	// => 1 ligne dans TBL_EVENT si l'�uipe est g�� par l'application
    	
    	$query  = "SELECT ID_TEAM FROM TEAM_HAS_COMPETITION ";
    	$query .= "WHERE ID_COMPETITION = $id_competition";
    	$result = mysql_query($query) or trigger_error(mysql_error(), E_USER_ERROR);
    	while ($row = mysql_fetch_assoc($result)) 
    	{
    		$team_dom = $_teammodeldao->queryTeamById($row["ID_TEAM"]);
    
    		echo "<br/>". $team_dom->getLibTeam();
    
    		// On parcourt les �uipes qu'elles vont accueillir
            $query  = "SELECT ID_TEAM FROM TEAM_HAS_COMPETITION ";
    		$query .= "WHERE ID_TEAM <> " . $team_dom->getIdTeam() . " AND ID_COMPETITION = ".$id_competition;
            $result2 = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
    	
            while ($row2 = mysql_fetch_assoc($result2)) 
    		{
                $team_vis = $_teammodeldao->queryTeamById($row2["ID_TEAM"]);
    			
    			// On insert un nouvel evenement
    			$id_event = 0;
    			$lib_event = "";
    
    			// si le match oppose une des �uipes g��s
    			if (($team_dom->getManaged() == 1) || ($team_vis->getManaged() == 1))
    			{
    				 $lib_event = $team_dom->getLibTeam() . " - " . $team_vis->getLibTeam();
    				
    				// insert de l'event
    				$query  = "INSERT INTO TBL_EVENT(LIB_EVENT, ID_TYP_EVENT) ";
    				$query .= "VALUES ('$lib_event', '2')";
    				$result3 = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
    				// recup de l'id_event
    				$id_event = mysql_insert_id();
    
                    if ($team_dom->getManaged() == 1)
		            {
                        $query  = "INSERT INTO EVENT_HAS_TEAM(ID_EVENT, ID_TEAM) ";
    			        $query .= "VALUES ('$id_event', '".$team_dom->getIdTeam()."')";
    			        $result3 = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
                    }

                    if ($team_vis->getManaged() == 1)
		            {
                        $query  = "INSERT INTO EVENT_HAS_TEAM(ID_EVENT, ID_TEAM) ";
    			        $query .= "VALUES ('$id_event', '".$team_vis->getIdTeam()."')";
    			        $result3 = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
                    }    				
    				
                    echo "<br/>". $team_dom->getLibTeam() . " - " . $team_vis->getIdTeam()." ".$row2["ID_TEAM"]." - ".$team_vis->getLibTeam() . " - event : " . $id_event;
    	
        			// On insert un nouveau match
        			$query  = "INSERT INTO TBL_MATCH (ID_TEAM_HOME, ID_TEAM_VISITOR, ID_EVENT, ID_COMPETITION) ";
        			$query .= "VALUES ('".$team_dom->getIdTeam()."', '".$team_vis->getIdTeam()."', $id_event, '$id_competition')";
        			$result3 = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
        			
        			}else {
        
        			echo "<br/>". $team_dom->getLibTeam() . " - " . $team_vis->getLibTeam() . " - event : " . $id_event;
        	
        			// On insert un nouveau match
        			$query  = "INSERT INTO TBL_MATCH (ID_TEAM_HOME, ID_TEAM_VISITOR,  ID_COMPETITION) ";
        			$query .= "VALUES ('".$team_dom->getIdTeam()."', '".$team_vis->getIdTeam()."', '$id_competition')";
        			$result3 = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
    			}
    			
    			$match_id = mysql_insert_id();
		    
        		// on insert une nouvelle journ� de championnat
        		$query = "INSERT INTO TBL_GAMEPLAY(ID_MATCH,ID_TEAM) VALUES('$match_id','".$team_dom->getIdTeam()."')";
        		$result4 = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
			   	
        		$query = "INSERT INTO TBL_GAMEPLAY(ID_MATCH, ID_TEAM) VALUES ('$match_id', '".$team_vis->getIdTeam()."')";
        		$result5 = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
    
    		
            } // fin while 1
        } // fin while 2
        
	}
    
	
}

?>
	
